package com.wy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wy.model.entity.User;
import com.wy.model.vo.RouterVo;
import com.wy.model.vo.UserQueryVo;
import com.wy.system.mapper.UserMapper;
import com.wy.system.service.PermissionService;
import com.wy.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  @Autowired
  private PermissionService permissionService;

  @Override
  public IPage<User> selectPage(Page<User> pageParam, UserQueryVo userQueryVo) {
    return baseMapper.selectPage(pageParam,userQueryVo);
  }

  //更改用户状态
  @Override
  public void updateStatus(Integer id, Integer status) {
    //根据用户id查询
    User user = baseMapper.selectById(id);
    //设置修改状态
    user.setStatus(status);
    //调用方法修改
    baseMapper.updateById(user);
  }

  //username查询
  @Override
  public User getUserInfoByUserName(String username) {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("username",username);
    return baseMapper.selectOne(wrapper);
  }

  @Override
  public Map<String, Object> getUserInfo(String username) {
    //根据username查询用户基本信息
    User user = this.getUserInfoByUserName(username);
    //根据userid查询菜单权限值
    List<RouterVo> routerVoList =  permissionService.getUserMenuList(user.getId());
    //根据userid查询按钮权限值
    List<String> buttonList = permissionService.getUserButtonList(user.getId());
    Map<String,Object> result = new HashMap<>();
    result.put("name",username);
    result.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    result.put("roles","[\"admin\"]");
    //菜单权限数据
    result.put("routers",routerVoList);
    //按钮权限数据
    result.put("buttons",buttonList);
    return result;
  }
}
